package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.PageUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.AlertDataPage;
import com.zzyl.nursing.dto.handleAlertDataDto;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }


    @Override
    public TableDataInfo<AlertData> myListByPage(AlertDataPage alertData) {

        // 使用PageUtils开启分页
        PageUtils.startPage();

        // 执行查询
        List<AlertData> list = alertDataMapper.myListByPage(alertData);

        // 封装分页结果
        return new TableDataInfo<>(list, (int) new PageInfo<>(list).getTotal());
    }

    /**
     * 处理报警数据
     * @param dto
     * @return
     */
    @Autowired
    private ISysUserService sysUserService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult handleAlertData(handleAlertDataDto dto) {
        //根据id查询数据库
        AlertData alertData = getById(dto.getId());
        //如果状态为1，则不能处理
        if (alertData.getStatus() == 1){
            throw new RuntimeException("该数据已处理");
        }
        List<AlertData> list = lambdaQuery()
                .eq(AlertData::getCreateTime, alertData.getCreateTime())
                .eq(AlertData::getIotId, alertData.getIotId())
                .list();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        String username = loginUser.getUsername();
        list.forEach(item -> {
            item.setStatus(1);
            item.setProcessorId(userId);
            item.setProcessorName(username);
        });
        updateBatchById(list);
        return AjaxResult.success();
    }

}
